package com.yswy.backstage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.backstage.service.AppProjectService;
import com.yswy.backstage.service.PackageManageService;
import com.yswy.backstage.service.SoftnessService;
import com.yswy.backstage.service.WhitelistAppUserService;
import com.yswy.common.utils.StringUtils;
import com.yswy.common.utils.http.HttpUtils;
import com.yswy.domain.common.BasePojo;
import com.yswy.domain.entity.AppProject;
import com.yswy.domain.entity.PackageManage;
import com.yswy.domain.entity.Softness;
import com.yswy.domain.entity.WhitelistAppUser;
import com.yswy.domain.entity.user.User;
import com.yswy.domain.framework.context.CurrentUserContext;
import com.yswy.domain.mapper.SoftnessMapper;
import com.yswy.domain.mapper.WhitelistAppUserMapper;
import com.yswy.domain.vo.dto.BaseFilter;
import com.yswy.domain.vo.gld.addOrUpate.SoftnessAddOrUpdate;
import com.yswy.domain.vo.gld.addOrUpate.WhitelistAppUserAddOrUpdate;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

import static com.yswy.backstage.utils.CrudUtils.selectCount;
import static com.yswy.common.constants.WarnMsgConstants.NULL_MSG;

@Slf4j
@Service
public class WhitelistAppUserServiceImpl extends ServiceImpl<WhitelistAppUserMapper, WhitelistAppUser>
        implements WhitelistAppUserService {
    @Autowired
    private AppProjectService appProjectService ;
    @Autowired
    private PackageManageService packageManageService ;
    @Override
    public Page<WhitelistAppUser> listWhitelistAppUser(BaseFilter req) {
        Page<WhitelistAppUser> WhitelistAppUsers = baseMapper.pageWhitelistAppUser(req
                , new Page<>(req.getPageNum() , req.getPageSize()) );
        return WhitelistAppUsers;
    }

    @Override
    public List<WhitelistAppUser> AllWhitelistAppUser() {
        return list(new LambdaQueryWrapper<WhitelistAppUser>()
                .eq(BasePojo::getIsDel , 0));
    }

    @Override
    public void addOrUpadteWhitelistAppUser(WhitelistAppUserAddOrUpdate res) {
        User user = CurrentUserContext.getCurrentUser();
        WhitelistAppUser whitelistAppUser = null ;
        if (res.getId() != null){
            whitelistAppUser = getById(res.getId());
            Assert.state(null != whitelistAppUser && null != whitelistAppUser.getId(), NULL_MSG);
            BeanUtil.copyProperties(res, whitelistAppUser);
            BasePojo.setBase(user.getId().longValue(), false, whitelistAppUser);
            updateById(whitelistAppUser);
        }else {
            whitelistAppUser = new WhitelistAppUser(res , user);
            save(whitelistAppUser);
        }
    }

    @Override
    public void delWhitelistAppUser(Long whitelistAppUserId) {
        User user = CurrentUserContext.getCurrentUser();
        Integer count = selectCount(baseMapper, s -> s.eq(BasePojo::getId, whitelistAppUserId));
        Assert.state(null != count && 0 < count, "未找到指定信息，请刷新重试");
        WhitelistAppUser bo = new WhitelistAppUser();
        bo.setId(whitelistAppUserId);
        bo.setIsDel(1);
        BasePojo.setBase(user.getId().longValue(), false, bo);
        updateById(bo);
    }

    @Override
    public void updarteVipAppUserVip(Long whitelistAppUserId, Long projectId) {
//        datePlatform/setUserVip
        PackageManage packageManage = packageManageService.getById(projectId);

        AppProject appProject = appProjectService.getById(packageManage.getProjectId());
        Assert.state(null != appProject, "未找到指定项目，请刷新重试");
        Assert.state(StringUtils.isNotEmpty(appProject.getDomainName()), "项目未配置域名");
        WhitelistAppUser whitelistAppUser = getById(whitelistAppUserId);
        Assert.state(null != whitelistAppUser, "未找到指定信息，请刷新重试");

        //发送请求
        String url =appProject.getDomainName()+"datePlatform/setUserVip" ;
        String req = JSONObject.toJSONString(whitelistAppUser);
        try {
            String body = HttpUtils.sendHttpPost(url , req);
            log.info("设置为vip响应："+body);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delAppUser(Long whitelistAppUserId, Long projectId) {
//        datePlatform/clearUser
        PackageManage packageManage = packageManageService.getById(projectId);

        AppProject appProject = appProjectService.getById(packageManage.getProjectId());
        Assert.state(null != appProject, "未找到指定项目，请刷新重试");
        Assert.state(StringUtils.isNotEmpty(appProject.getDomainName()), "项目未配置域名");
        WhitelistAppUser whitelistAppUser = getById(whitelistAppUserId);
        Assert.state(null != whitelistAppUser, "未找到指定信息，请刷新重试");

        //发送请求
        String url =appProject.getDomainName()+"datePlatform/clearUser" ;
        String req = JSONObject.toJSONString(whitelistAppUser);
        try {
            String body = HttpUtils.sendHttpPost(url , req);
            log.info("清除用户数据响应："+body);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
